﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>教师信息管理</title>
    <link href="${pageContext.request.contextPath}/resources/bootstrap5/css/bootstrap.min.css" rel="stylesheet">
    <!-- 使用国内稳定的BootCDN Bootstrap Icons -->
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <!-- 备用CDN链接 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
    <style>
        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            margin: 0;
            padding: 0;
        }
        .header {
            background: #2196f3;
            color: #fff;
            padding: 0 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 56px;
            box-shadow: 0 2px 8px rgba(33,150,243,0.08);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
        }
        .header-left {
            display: flex;
            align-items: center;
        }
        .header-logo {
            width: 36px;
            height: 36px;
            margin-right: 12px;
        }
        .header-title {
            font-size: 1.4rem;
            font-weight: bold;
            letter-spacing: 1px;
        }
        .header-nav {
            margin-left: 32px;
            font-size: 1rem;
        }
        .header-nav a {
            color: #fff;
            text-decoration: none;
            margin-right: 16px;
            opacity: 0.85;
        }
        .header-nav a.active, .header-nav a:hover {
            opacity: 1;
            font-weight: bold;
        }
        .sidebar {
            position: fixed;
            top: 56px;
            left: 0;
            bottom: 0;
            width: 220px;
            background: #fff;
            box-shadow: 2px 0 8px rgba(33,150,243,0.06);
            overflow-y: auto;
            z-index: 100;
            padding-top: 16px;
        }
        .sidebar-menu {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .sidebar-menu > li {
            margin-bottom: 4px;
        }
        .sidebar-menu > li > a {
            display: flex;
            align-items: center;
            padding: 10px 24px;
            color: #333;
            text-decoration: none;
            border-radius: 8px 0 0 8px;
            transition: background 0.2s, color 0.2s;
            font-size: 1rem;
        }
        .sidebar-menu > li > a:hover, .sidebar-menu > li > a.active {
            background: #e3f2fd;
            color: #2196f3;
            font-weight: bold;
        }
        .sidebar-menu > li > a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 1.1rem;
        }
        .sidebar-submenu {
            list-style: none;
            padding-left: 40px;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
        }
        .sidebar-menu > li.open .sidebar-submenu {
            max-height: 500px;
        }
        .sidebar-submenu > li > a {
            display: block;
            padding: 7px 0;
            color: #666;
            text-decoration: none;
            transition: color 0.2s;
            font-size: 0.98rem;
        }
        .sidebar-submenu > li > a:hover, .sidebar-submenu > li > a.active {
            color: #2196f3;
            font-weight: 500;
        }
        .content {
            margin-left: 240px;
            padding: 80px 32px 32px;
        }
        .card {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(33,150,243,0.07);
            margin-bottom: 24px;
            border: none;
        }
        .card-body {
            padding: 24px 24px 16px 24px;
        }
        .search-bar {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 18px;
            flex-wrap: nowrap;
        }
        .search-bar input {
            border-radius: 8px;
            border: 1px solid #e3e3e3;
            padding: 6px 14px;
            font-size: 1rem;
            flex: 1;
            min-width: 180px;
        }
        .search-bar .btn {
            border-radius: 8px;
            font-size: 1rem;
            padding: 6px 18px;
            font-weight: 500;
            box-shadow: none;
            flex-shrink: 0;
        }
        .btn-primary {
            background: #2196f3;
            border: none;
        }
        .btn-primary:hover {
            background: #1976d2;
        }
        .btn-warning {
            background: #ffb300;
            color: #fff;
            border: none;
        }
        .btn-warning:hover {
            background: #ff9800;
            color: #fff;
        }
        .btn-success {
            background: #43a047;
            border: none;
        }
        .btn-success:hover {
            background: #388e3c;
        }
        .btn-danger {
            background: #fff0f0;
            color: #e53935;
            border: 1px solid #ffcdd2;
        }
        .btn-danger:hover {
            background: #ffcdd2;
            color: #b71c1c;
        }
        .table {
            background: #fff;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(33,150,243,0.04);
        }
        .table thead th {
            background: #f5f7fa;
            color: #2196f3;
            font-weight: bold;
            text-align: center;
            border-bottom: 2px solid #e3e3e3;
        }
        .table tbody tr {
            transition: background 0.2s;
        }
        .table tbody tr:hover {
            background: #f1f8ff;
        }
        .table td, .table th {
            vertical-align: middle !important;
            text-align: center;
        }
        .pagination {
            justify-content: flex-start;
            margin-top: 12px;
        }
        .pagination .page-item .page-link {
            border-radius: 6px !important;
            margin: 0 2px;
            color: #2196f3;
            border: none;
            background: #fff;
            font-weight: 500;
            transition: background 0.2s, color 0.2s;
        }
        .pagination .page-item.active .page-link {
            background: #2196f3;
            color: #fff;
            font-weight: bold;
            border: none;
        }
        .pagination .page-link:hover {
            background: #e3f2fd;
            color: #2196f3;
        }
        .form-control {
            border-radius: 8px;
            border: 1px solid #e3e3e3;
        }
        .modal-content {
            border-radius: 12px;
        }
        
        /* 用户下拉菜单样式 */
        .header-user {
            display: flex;
            align-items: center;
            position: relative;
        }
        .header-user-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 8px;
            object-fit: cover;
            background: #fff;
            cursor: pointer;
            transition: transform 0.2s;
        }
        .header-user-avatar:hover {
            transform: scale(1.1);
        }
        .header-user-name {
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            min-width: 160px;
            z-index: 1001;
            opacity: 0;
            visibility: hidden;
            transform: translateY(-10px);
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        .user-dropdown.show {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        .user-dropdown::before {
            content: '';
            position: absolute;
            top: -6px;
            right: 20px;
            width: 12px;
            height: 12px;
            background: #fff;
            transform: rotate(45deg);
            box-shadow: -2px -2px 4px rgba(0,0,0,0.1);
        }
        .user-dropdown-item {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            color: #333;
            text-decoration: none;
            transition: background 0.2s;
            border-bottom: 1px solid #f0f0f0;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown-item:last-child {
            border-bottom: none;
        }
        .user-dropdown-item:hover {
            background: #f8f9fa;
            color: #2196f3;
        }
        .user-dropdown-item i {
            margin-right: 8px;
            width: 16px;
            text-align: center;
        }
        
        @media (max-width: 900px) {
            .content {
                margin-left: 0;
                padding: 80px 10px 10px;
            }
            .sidebar {
                width: 100%;
                position: static;
                box-shadow: none;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-left">
            <img src="${pageContext.request.contextPath}/resources/images/logo.png" alt="Logo" class="header-logo">
            <span class="header-title">教务管理系统</span>
            <div class="header-nav">
                <a href="${pageContext.request.contextPath}/admin/home">首页</a>
                <span style="opacity:0.5;">/</span>
                <a href="#" class="active">教师信息管理</a>
            </div>
        </div>
        
        <!-- 用户下拉菜单 -->
        <div class="header-user" id="userDropdownContainer">
            <img src="${pageContext.request.contextPath}/resources/images/baimao.png" class="header-user-avatar" alt="用户头像" id="userAvatar">
            <span class="header-user-name" id="userName">
                <c:choose>
                    <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.realName}">
                        ${sessionScope.loggedInUser.realName}
                    </c:when>
                    <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.username}">
                        ${sessionScope.loggedInUser.username}
                    </c:when>
                    <c:otherwise>
                        系统管理员
                    </c:otherwise>
                </c:choose>
            </span>
            <div class="user-dropdown" id="userDropdown">
                <a href="#" class="user-dropdown-item" id="profileBtn">
                    <i class="bi bi-person-circle"></i>
                    个人中心
                </a>
                <a href="${pageContext.request.contextPath}/user/logout" class="user-dropdown-item">
                    <i class="bi bi-box-arrow-right"></i>
                    退出系统
                </a>
            </div>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="sidebar">
        <ul class="sidebar-menu">
            <li>
                <a href="${pageContext.request.contextPath}/admin/home" class="active">
                    <i class="bi bi-house-door"></i> 系统首页
                </a>
            </li>
            <li>
                <a href="#" class="has-submenu">
                    <i class="bi bi-megaphone"></i> 信息公告
                </a>
                <ul class="sidebar-submenu">
                    <li><a href="${pageContext.request.contextPath}/admin/notifications/academic">教学通知</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/notifications/exam">考试安排</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/notifications/classroom">教室安排</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="has-submenu">
                    <i class="bi bi-building"></i> 行政管理
                </a>
                <ul class="sidebar-submenu">
                    <li><a href="${pageContext.request.contextPath}/admin/college">学院信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/major">专业信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/class">班级信息</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="has-submenu">
                    <i class="bi bi-book"></i> 教学管理
                </a>
                <ul class="sidebar-submenu">
                    <li><a href="${pageContext.request.contextPath}/admin/course">课程信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/course-selection">选课管理</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/score">成绩管理</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/online">网上评教</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="has-submenu">
                    <i class="bi bi-calendar3"></i> 教务管理
                </a>
                <ul class="sidebar-submenu">
                    <li><a href="${pageContext.request.contextPath}/admin/leave">请假管理</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/homework">作业提交</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/attendance">考勤信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/online">网上评教</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="has-submenu">
                    <i class="bi bi-person"></i> 用户管理
                </a>
                <ul class="sidebar-submenu">
                    <li><a href="${pageContext.request.contextPath}/admin/admin">管理员信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/teacher">教师信息</a></li>
                    <li><a href="${pageContext.request.contextPath}/admin/student">学生信息</a></li>
                </ul>
            </li>
            <li>
                <a href="${pageContext.request.contextPath}/user/logout">
                    <i class="bi bi-box-arrow-right"></i> 退出系统
                </a>
            </li>
        </ul>
    </div>

    <!-- 主要内容区 -->
    <div class="content">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
            <h1 class="h2">教师信息管理</h1>
        </div>

        <div class="card">
            <div class="card-body">
                <!-- 搜索栏 -->
                <form class="search-bar" method="get" action="${pageContext.request.contextPath}/admin/teacher">
                    <input type="text" class="form-control" name="teacherSearch" value="${param.teacherSearch}" placeholder="请输入教师姓名或工号进行查询">
                    <button type="submit" class="btn btn-primary">查询</button>
                    <a href="${pageContext.request.contextPath}/admin/teacher" class="btn btn-warning">重置</a>
                </form>

                <!-- 操作按钮 -->
                <div class="mb-3" style="display:flex;gap:12px;">
                    <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addTeacherModal">
                        新增教师
                    </button>
                    <button type="button" class="btn btn-danger" id="batchDeleteBtn">
                        批量删除
                    </button>
                </div>

                <!-- 教师信息列表 -->
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th style="width: 5%;"><input type="checkbox" id="selectAllCheckbox"></th>
                            <th style="width: 8%;">序号</th>
                            <th style="width: 12%;">工号</th>
                            <th style="width: 12%;">姓名</th>
                            <th style="width: 8%;">性别</th>
                            <th style="width: 15%;">专业</th>
                            <th style="width: 15%;">职称</th>
                            <th style="width: 10%;">联系电话</th>
                            <th style="width: 12%;">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:if test="${empty teachers}">
                            <tr>
                                <td colspan="9" class="text-center">暂无教师信息</td>
                            </tr>
                        </c:if>
                        <c:if test="${not empty teachers}">
                            <c:forEach var="teacher" items="${teachers}" varStatus="status">
                                <tr>
                                    <td><input type="checkbox" class="form-check-input item-checkbox" value="${teacher.id}"></td>
                                    <td>${status.count + (currentPage - 1) * pageSize}</td>
                                    <td><c:out value="${teacher.teacherNo}"/></td>
                                    <td>
                                        <c:if test="${not empty teacher.user and not empty teacher.user.realName}">
                                            <c:out value="${teacher.user.realName}"/>
                                        </c:if>
                                        <c:if test="${empty teacher.user or empty teacher.user.realName}">
                                            未填写
                                        </c:if>
                                    </td>
                                    <td>
                                        <c:choose>
                                            <c:when test="${teacher.gender == 1}">
                                                <span class="badge bg-primary">男</span>
                                            </c:when>
                                            <c:when test="${teacher.gender == 0}">
                                                <span class="badge bg-info">女</span>
                                            </c:when>
                                            <c:otherwise>
                                                <span class="badge bg-secondary">未知</span>
                                            </c:otherwise>
                                        </c:choose>
                                    </td>
                                    <td>
                                        <c:if test="${not empty teacher.college and not empty teacher.college.name}">
                                            <c:out value="${teacher.college.name}"/>
                                        </c:if>
                                        <c:if test="${empty teacher.college or empty teacher.college.name}">
                                            未分配
                                        </c:if>
                                    </td>
                                    <td>
                                        <c:if test="${not empty teacher.title}">
                                            <c:out value="${teacher.title}"/>
                                        </c:if>
                                        <c:if test="${empty teacher.title}">
                                            未设置
                                        </c:if>
                                    </td>
                                    <td>
                                        <c:if test="${not empty teacher.phone}">
                                            <c:out value="${teacher.phone}"/>
                                        </c:if>
                                        <c:if test="${empty teacher.phone}">
                                            未设置
                                        </c:if>
                                    </td>
                                    <td>
                                        <button class="btn btn-primary btn-sm view-btn" data-id="${teacher.id}">查看</button>
                                        <button class="btn btn-warning btn-sm edit-btn" data-id="${teacher.id}">编辑</button>
                                        <button class="btn btn-danger btn-sm delete-btn" data-id="${teacher.id}">删除</button>
                                    </td>
                                </tr>
                            </c:forEach>
                        </c:if>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <nav aria-label="Page navigation">
                    <ul class="pagination justify-content-center">
                        <c:if test="${not empty currentPage and currentPage > 1}">
                            <li class="page-item">
                                <a class="page-link" href="${pageContext.request.contextPath}/admin/teacher?page=${currentPage - 1}&size=${pageSize}&teacherSearch=${teacherSearch}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        </c:if>

                        <c:if test="${not empty totalPages}">
                            <c:forEach begin="1" end="${totalPages}" var="i">
                                <li class="page-item ${currentPage == i ? 'active' : ''}">
                                    <a class="page-link" href="${pageContext.request.contextPath}/admin/teacher?page=${i}&size=${pageSize}&teacherSearch=${teacherSearch}">${i}</a>
                                </li>
                            </c:forEach>
                        </c:if>

                        <c:if test="${not empty currentPage and not empty totalPages and currentPage < totalPages}">
                            <li class="page-item">
                                <a class="page-link" href="${pageContext.request.contextPath}/admin/teacher?page=${currentPage + 1}&size=${pageSize}&teacherSearch=${teacherSearch}" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </c:if>
                    </ul>
                    <div class="text-left mt-2" style="font-size:0.98rem;color:#888;">
                        共 ${totalRecords} 条
                    </div>
                </nav>
            </div>
        </div>
    </div>

    <!-- 添加/编辑教师模态框 -->
    <div class="modal fade" id="addTeacherModal" tabindex="-1" aria-labelledby="addTeacherModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addTeacherModalLabel">添加教师</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="addTeacherForm">
                        <input type="hidden" name="id" id="teacherId">
                        <input type="hidden" name="userId" id="userId">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="teacherNo" class="form-label">工号</label>
                                    <input type="text" class="form-control" id="teacherNo" name="teacherNo" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="realName" class="form-label">真实姓名</label>
                                    <input type="text" class="form-control" id="realName" name="realName" required>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="username" name="username" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="password" class="form-label">密码</label>
                                    <input type="password" class="form-control" id="password" name="password" required>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="gender" class="form-label">性别</label>
                                    <select class="form-control" id="gender" name="gender" required>
                                        <option value="">请选择性别</option>
                                        <option value="1">男</option>
                                        <option value="0">女</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="phone" class="form-label">联系电话</label>
                                    <input type="text" class="form-control" id="phone" name="phone">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="collegeId" class="form-label">学院</label>
                                    <select class="form-control" id="collegeId" name="collegeId">
                                        <option value="">请选择学院</option>
                                        <c:forEach var="major" items="${majorList}">
                                            <option value="${major.college.id}">
                                                <c:out value="${major.college.name}"/>
                                            </option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="title" class="form-label">职称</label>
                                    <input type="text" class="form-control" id="title" name="title" placeholder="如：讲师、副教授、教授">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="submitTeacher">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 查看教师详情模态框 -->
    <div class="modal fade" id="viewTeacherModal" tabindex="-1" aria-labelledby="viewTeacherModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="viewTeacherModalLabel">教师详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <p><strong>工号：</strong><span id="viewTeacherNo"></span></p>
                            <p><strong>姓名：</strong><span id="viewRealName"></span></p>
                            <p><strong>用户名：</strong><span id="viewUsername"></span></p>
                            <p><strong>性别：</strong><span id="viewGender"></span></p>
                        </div>
                        <div class="col-md-6">
                            <p><strong>专业：</strong><span id="viewMajor"></span></p>
                            <p><strong>职称：</strong><span id="viewTitle"></span></p>
                            <p><strong>联系电话：</strong><span id="viewPhone"></span></p>
                            <p><strong>创建时间：</strong><span id="viewCreateTime"></span></p>
                        </div>
                    </div>
                    
                    <!-- 教学统计信息 -->
                    <div class="mt-4">
                        <h6>教学统计</h6>
                        <div class="row">
                            <div class="col-md-4">
                                <div class="card border-primary">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-primary" id="totalCourses">0</h5>
                                        <p class="card-text">授课总数</p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="card border-success">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-success" id="totalStudents">0</h5>
                                        <p class="card-text">学生总数</p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="card border-warning">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-warning" id="averageScore">0</h5>
                                        <p class="card-text">平均评分</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 个人中心模态框 -->
    <div class="modal fade" id="profileModal" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="profileModalLabel">个人中心</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <ul class="nav nav-tabs" id="profileTabs" role="tablist">
                        <li class="nav-item">
                            <button class="nav-link active" id="profile-info-tab" data-bs-toggle="tab" data-bs-target="#profile-info" type="button" role="tab" aria-controls="profile-info" aria-selected="true">
                                <i class="bi bi-person"></i> 个人信息
                            </button>
                        </li>
                        <li class="nav-item">
                            <button class="nav-link" id="change-password-tab" data-bs-toggle="tab" data-bs-target="#change-password" type="button" role="tab" aria-controls="change-password" aria-selected="false">
                                <i class="bi bi-key"></i> 修改密码
                            </button>
                        </li>
                    </ul>
                    <div class="tab-content" id="profileTabContent">
                        <div class="tab-pane fade show active" id="profile-info" role="tabpanel" aria-labelledby="profile-info-tab">
                            <form id="profileForm" class="mt-3">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="profileUsername" class="form-label">用户名</label>
                                            <input type="text" class="form-control" id="profileUsername" readonly>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="profileRole" class="form-label">角色</label>
                                            <input type="text" class="form-control" id="profileRole" readonly>
                                        </div>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label for="profileRealName" class="form-label">真实姓名</label>
                                    <input type="text" class="form-control" id="profileRealName" required>
                                </div>
                                <div class="d-grid gap-2">
                                    <button type="button" class="btn btn-primary" id="updateProfileBtn">
                                        <i class="bi bi-check-circle"></i> 更新信息
                                    </button>
                                </div>
                            </form>
                        </div>
                        <div class="tab-pane fade" id="change-password" role="tabpanel" aria-labelledby="change-password-tab">
                            <form id="passwordForm" class="mt-3">
                                <div class="mb-3">
                                    <label for="oldPassword" class="form-label">原密码</label>
                                    <input type="password" class="form-control" id="oldPassword" required>
                                </div>
                                <div class="mb-3">
                                    <label for="newPassword" class="form-label">新密码</label>
                                    <input type="password" class="form-control" id="newPassword" required minlength="6">
                                    <div class="form-text">密码长度至少6位</div>
                                </div>
                                <div class="mb-3">
                                    <label for="confirmPassword" class="form-label">确认新密码</label>
                                    <input type="password" class="form-control" id="confirmPassword" required>
                                </div>
                                <div class="d-grid gap-2">
                                    <button type="button" class="btn btn-warning" id="changePasswordBtn">
                                        <i class="bi bi-key"></i> 修改密码
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="${pageContext.request.contextPath}/resources/bootstrap5/jquery-3.6.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/resources/bootstrap5/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            // 用户名下拉菜单功能
            let userDropdownTimeout;

            $('#userDropdownContainer').hover(
                function() {
                    clearTimeout(userDropdownTimeout);
                    $('#userDropdown').addClass('show');
                },
                function() {
                    userDropdownTimeout = setTimeout(function() {
                        $('#userDropdown').removeClass('show');
                    }, 300);
                }
            );

            // 点击头像或用户名也可以显示下拉菜单
            $('#userAvatar, #userName').click(function(e) {
                e.preventDefault();
                $('#userDropdown').toggleClass('show');
            });

            // 点击其他地方隐藏下拉菜单
            $(document).click(function(e) {
                if (!$(e.target).closest('#userDropdownContainer').length) {
                    $('#userDropdown').removeClass('show');
                }
            });

            // 个人中心功能
            $('#profileBtn').click(function(e) {
                e.preventDefault();
                $('#userDropdown').removeClass('show');
                loadUserProfile();
                $('#profileModal').modal('show');
            });

            // 加载用户个人信息
            function loadUserProfile() {
                $.ajax({
                    url: '${pageContext.request.contextPath}/user/profile',
                    type: 'GET',
                    success: function(response) {
                        if (response.success) {
                            const user = response.user;
                            $('#profileUsername').val(user.username);
                            $('#profileRealName').val(user.realName || '');

                            // 设置角色显示
                            let roleText = '';
                            switch(user.role) {
                                case 1: roleText = '管理员'; break;
                                case 2: roleText = '教师'; break;
                                case 3: roleText = '学生'; break;
                                default: roleText = '未知'; break;
                            }
                            $('#profileRole').val(roleText);
                        } else {
                            alert('获取用户个人信息失败: ' + response.message);
                        }
                    },
                    error: function() {
                        alert('获取用户个人信息失败，请重试');
                    }
                });
            }

            // 更新个人信息
            $('#updateProfileBtn').click(function() {
                const realName = $('#profileRealName').val().trim();
                if (!realName) {
                    alert('请输入真实姓名');
                    return;
                }

                $.ajax({
                    url: '${pageContext.request.contextPath}/user/updateProfile',
                    type: 'POST',
                    data: {
                        realName: realName
                    },
                    success: function(response) {
                        if (response.success) {
                            alert('个人信息更新成功');
                            // 更新页面显示的用户名
                            $('#userName').text(realName);
                            $('#profileModal').modal('hide');
                            // 刷新页面以更新所有显示的用户名
                            setTimeout(function() {
                                location.reload();
                            }, 1000);
                        } else {
                            alert('更新失败: ' + response.message);
                        }
                    },
                    error: function() {
                        alert('更新失败，请重试');
                    }
                });
            });

            // 修改密码
            $('#changePasswordBtn').click(function() {
                const oldPassword = $('#oldPassword').val();
                const newPassword = $('#newPassword').val();
                const confirmPassword = $('#confirmPassword').val();

                if (!oldPassword || !newPassword || !confirmPassword) {
                    alert('请填写完整的密码信息');
                    return;
                }

                if (newPassword.length < 6) {
                    alert('新密码长度不能少于6位');
                    return;
                }

                if (newPassword !== confirmPassword) {
                    alert('新密码和确认密码不一致');
                    return;
                }

                $.ajax({
                    url: '${pageContext.request.contextPath}/user/changePassword',
                    type: 'POST',
                    data: {
                        oldPassword: oldPassword,
                        newPassword: newPassword,
                        confirmPassword: confirmPassword
                    },
                    success: function(response) {
                        if (response.success) {
                            alert('密码修改成功');
                            $('#passwordForm')[0].reset();
                            $('#profileModal').modal('hide');
                        } else {
                            alert('修改失败: ' + response.message);
                        }
                    },
                    error: function() {
                        alert('修改失败，请重试');
                    }
                });
            });

            // 模态框关闭时重置表单
            $('#profileModal').on('hidden.bs.modal', function() {
                $('#profileForm')[0].reset();
                $('#passwordForm')[0].reset();
                // 切换回个人信息标签
                $('#profile-info-tab').tab('show');
            });
        });
    </script>
    <script>
        $(document).ready(function() {
            // 用户下拉菜单功能
            let userDropdownTimeout;

            $('#userDropdownContainer').hover(
                function() {
                    clearTimeout(userDropdownTimeout);
                    $('#userDropdown').addClass('show');
                },
                function() {
                    userDropdownTimeout = setTimeout(function() {
                        $('#userDropdown').removeClass('show');
                    }, 300);
                }
            );

            // 点击头像或用户名也可以显示下拉菜单
            $('#userAvatar, #userName').click(function(e) {
                e.preventDefault();
                $('#userDropdown').toggleClass('show');
            });

            // 点击其他地方隐藏下拉菜单
            $(document).click(function(e) {
                if (!$(e.target).closest('#userDropdownContainer').length) {
                    $('#userDropdown').removeClass('show');
                }
            });
        });
    </script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 侧边栏子菜单展开/收起
            const submenuLinks = document.querySelectorAll('.has-submenu');
            submenuLinks.forEach(link => {
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    const parent = this.parentElement;
                    parent.classList.toggle('open');
                });
            });

            // 初始化时展开当前活动菜单
            document.querySelectorAll('.sidebar-menu > li > a.active').forEach(activeLink => {
                const parent = activeLink.parentElement;
                if (activeLink.classList.contains('has-submenu')) {
                    parent.classList.add('open');
                }
            });

            // 查找子菜单中的活动链接并展开父菜单
            document.querySelectorAll('.sidebar-submenu > li > a.active').forEach(activeSubLink => {
                const parent = activeSubLink.parentElement.parentElement.parentElement;
                parent.classList.add('open');
            });
        });

        $(document).ready(function() {
            // 全选/取消全选
            $("#selectAllCheckbox").change(function() {
                $(".item-checkbox").prop('checked', $(this).prop('checked'));
                updateBatchDeleteButton();
            });

            // 单个复选框改变时更新全选状态
            $(document).on('change', '.item-checkbox', function() {
                const totalCheckboxes = $('.item-checkbox').length;
                const checkedCheckboxes = $('.item-checkbox:checked').length;
                
                $("#selectAllCheckbox").prop('checked', totalCheckboxes === checkedCheckboxes);
                updateBatchDeleteButton();
            });

            // 更新批量删除按钮状态
            function updateBatchDeleteButton() {
                const checkedCount = $('.item-checkbox:checked').length;
                $('#batchDeleteBtn').prop('disabled', checkedCount === 0);
            }

            // 批量删除
            $("#batchDeleteBtn").click(function() {
                var selectedIds = [];
                $(".item-checkbox:checked").each(function() {
                    selectedIds.push($(this).val());
                });

                if(selectedIds.length === 0) {
                    alert("请至少选择一项进行删除");
                    return;
                }

                if (confirm("确定要删除选中的 " + selectedIds.length + " 条教师记录吗？此操作不可恢复！")) {
                    window.location.href = "${pageContext.request.contextPath}/admin/teacher/batchDelete?ids=" + selectedIds.join(",");
                }
            });

            // 编辑按钮
            $(".edit-btn").click(function() {
                var id = $(this).data("id");
                $.get("${pageContext.request.contextPath}/admin/teacher/view/" + id, function(response) {
                    if(response.success) {
                        const teacher = response.teacher;
                        $("#addTeacherModalLabel").text("编辑教师");
                        $("#teacherNo").val(teacher.teacherNo);
                        
                        if (teacher.user) {
                            $("#realName").val(teacher.user.realName);
                            $("#username").val(teacher.user.username);
                            $("#userId").val(teacher.user.id);
                            $("#password").removeAttr('required').attr('placeholder', '不修改请留空');
                        }
                        
                        $("#gender").val(teacher.gender);
                        $("#phone").val(teacher.phone);
                        $("#title").val(teacher.title);
                        
                        if (teacher.college && teacher.college.id) {
                            $("#collegeId").val(teacher.college.id);
                        }
                        
                        $("#teacherId").val(teacher.id);
                        $("#addTeacherModal").modal("show");
                    } else {
                        alert("获取教师信息失败：" + response.message);
                    }
                }).fail(function() {
                    alert("获取教师信息失败，请重试");
                });
            });

            // 查看详情按钮
            $(".view-btn").click(function() {
                var id = $(this).data("id");
                $.get("${pageContext.request.contextPath}/admin/teacher/view/" + id, function(response) {
                    if(response.success) {
                        const teacher = response.teacher;
                        $("#viewTeacherNo").text(teacher.teacherNo);
                        
                        if (teacher.user) {
                            $("#viewRealName").text(teacher.user.realName || '未填写');
                            $("#viewUsername").text(teacher.user.username);
                        } else {
                            $("#viewRealName").text('未填写');
                            $("#viewUsername").text('未设置');
                        }
                        
                        $("#viewGender").text(teacher.gender === 1 ? '男' : (teacher.gender === 0 ? '女' : '未知'));
                        $("#viewPhone").text(teacher.phone || '未设置');
                        $("#viewTitle").text(teacher.title || '未设置');
                        
                        if (teacher.college) {
                            $("#viewMajor").text(teacher.college.name || '未分配');
                        } else {
                            $("#viewMajor").text('未分配');
                        }
                        
                        $("#viewCreateTime").text(teacher.createTime || '未知');
                        
                        // 加载教师统计信息
                        loadTeacherStats(id);
                        
                        $("#viewTeacherModal").modal("show");
                    } else {
                        alert("获取教师信息失败：" + response.message);
                    }
                }).fail(function() {
                    alert("获取教师信息失败，请重试");
                });
            });

            // 提交添加教师表单
            $("#submitTeacher").click(function() {
                // 基本验证
                if (!$("#teacherNo").val().trim()) {
                    alert("请输入工号");
                    return;
                }
                if (!$("#realName").val().trim()) {
                    alert("请输入真实姓名");
                    return;
                }
                if (!$("#username").val().trim()) {
                    alert("请输入用户名");
                    return;
                }
                if (!$("#gender").val()) {
                    alert("请选择性别");
                    return;
                }
                
                // 如果是添加操作，密码是必填的
                if (!$("#teacherId").val() && !$("#password").val().trim()) {
                    alert("请输入密码");
                    return;
                }
                
                // 获取表单数据
                var formData = {
                    teacherNo: $("#teacherNo").val().trim(),
                    username: $("#username").val().trim(),
                    realName: $("#realName").val().trim(),
                    password: $("#password").val().trim(),
                    gender: $("#gender").val(),
                    phone: $("#phone").val().trim(),
                    title: $("#title").val().trim(),
                    collegeId: $("#collegeId").val()
                };
                
                // 如果是编辑操作，添加id和userId
                if ($("#teacherId").val()) {
                    formData.id = $("#teacherId").val();
                    formData.userId = $("#userId").val();
                    
                    // 发送Ajax请求
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/teacher/edit",
                        type: "POST",
                        data: formData,
                        success: function(response) {
                            if (response.success) {
                                alert("编辑教师成功");
                                window.location.href = "${pageContext.request.contextPath}/admin/teacher";
                            } else {
                                alert("编辑教师失败：" + response.message);
                            }
                        },
                        error: function() {
                            alert("编辑教师请求失败，请重试");
                        }
                    });
                } else {
                    // 发送Ajax请求
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/teacher/add",
                        type: "POST",
                        data: formData,
                        success: function(response) {
                            if (response.success) {
                                alert("新增教师成功");
                                window.location.href = "${pageContext.request.contextPath}/admin/teacher";
                            } else {
                                alert("新增教师失败：" + response.message);
                            }
                        },
                        error: function() {
                            alert("新增教师请求失败，请重试");
                        }
                    });
                }
            });

            // 删除教师
            $(".delete-btn").click(function() {
                var id = $(this).data("id");
                if(confirm("确定要删除这个教师吗？")) {
                    window.location.href = "${pageContext.request.contextPath}/admin/teacher/delete/" + id;
                }
            });

            // 模态框关闭时重置表单
            $('#addTeacherModal').on('hidden.bs.modal', function() {
                $("#addTeacherModalLabel").text("添加教师");
                $("#addTeacherForm")[0].reset();
                $("#teacherId").val("");
                $("#userId").val("");
                $("#password").attr('required', 'required').removeAttr('placeholder');
            });
        });

        // 加载教师统计信息
        function loadTeacherStats(teacherId) {
            $.get("${pageContext.request.contextPath}/admin/teacher/stats/" + teacherId, function(response) {
                if(response.success) {
                    $("#totalCourses").text(response.data.totalCourses || 0);
                    $("#totalStudents").text(response.data.totalStudents || 0);
                    $("#averageScore").text(response.data.averageScore || 0);
                } else {
                    $("#totalCourses").text(0);
                    $("#totalStudents").text(0);
                    $("#averageScore").text(0);
                }
            }).fail(function() {
                $("#totalCourses").text(0);
                $("#totalStudents").text(0);
                $("#averageScore").text(0);
            });
        }
    </script>
</body>
</html> 

